package com.lx.user.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.lx.bean.Result;
import com.lx.bilibili.entity.User;
import com.lx.bilibili.entity.UserEx;
import com.lx.bilibili.entity.Video;
import com.lx.user.dao.UserExMapper;
import com.lx.user.dao.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2025-03-26
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserExMapper userExMapper;

    @Autowired
    UserMapper userMapper;


    @RequestMapping("logins")
    public Result login(String username, String password) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getName, username);
        lqw.eq(User::getPassword, password);
        User user = userMapper.selectOne(lqw);
        if (user != null) {
            return new Result(1, "登录成功", user);
        }
        return new Result(0, "登录失败", null);

    }

    @RequestMapping("searchUInfo")
    public UserEx searchUInfo(String searchText){
        LambdaQueryWrapper<UserEx> lqw = new LambdaQueryWrapper<>();
        lqw.like(UserEx::getName, searchText);
        lqw.orderByDesc(UserEx::getId);
        Page page = new PageDTO(1,1);
        List<UserEx> list = userExMapper.selectPage(page, lqw).getRecords();
        if(list.isEmpty()){
            return null;
        }
        for(UserEx user : list){
            user.setVideoNum(userExMapper.getVideoNumById(user.getId()));
        }
        return list.get(0);
    }

    @RequestMapping("getVideoById")
    public List<Video> getVideoById(int id){
        return userExMapper.getVideoById(id);
    }


    @RequestMapping("loginByEmail")
    public Result loginByEmail(String code, String email, HttpSession session){
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getEmail, email);
        User user = new User();
        String vcode = (String) session.getAttribute("vcode");
        System.out.println("code = " + code);
        Long timeout = (Long) session.getAttribute("vcode-timeout");
        if(vcode.equals(code)){
            return new Result(0, "验证码不正确", null);
        }
        if(timeout < System.currentTimeMillis()){
            return new Result(2, "验证码超时, 请重新验证", null);
        }
        User existUser = userMapper.selectOne(lqw);
        if(existUser == null){
            user.setEmail(email);
            user.setName(email.split("@")[0]);
            user.setPassword("123456");
            user.setHeadimg("https://ts1.tc.mm.bing.net/th/id/OIP-C.anuNpOP6-GznrWxynpl13gAAAA?w=208&h=211&c=8&rs=1&qlt=90&o=6&dpr=2.2&pid=3.1&rm=2");
            user.setRsetime(LocalDateTime.now());
            userMapper.insert(user);
            return new Result(1, "验证成功", user);

        }
        return new Result(1, "验证成功", existUser);
    }

    @RequestMapping("getUserFensAndSub")
    public Result getUserFensAndSub(int id){
        return new Result(1, "获取成功", userMapper.selectById(id));
    }

    @RequestMapping("forgetPassword")
    public Result forgetPassword(String email, String code, String password, String id , HttpSession session){
        System.out.println("email = " + email);
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getEmail, email);
        lqw.eq(User::getId, id);
        User user = userMapper.selectOne(lqw);
        System.out.println("user = " + user);
        if(user == null){
            return new Result(0, "用户不存在", null);
        }
        String vcode = (String) session.getAttribute("vcode");
        Long timeout = (Long) session.getAttribute("vcode-timeout");
        if(!vcode.equals(code)){
            return new Result(0, "验证码不正确", null);
        }
        if(timeout < System.currentTimeMillis()){
            return new Result(2, "验证码超时, 请重新验证", null);
        }
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getEmail, email).set(User::getPassword, password);
        try{
            if (userMapper.update(user, updateWrapper) > 0) {
                return new Result(1, "修改成功", null);
            }
        }catch(Exception e){
            return new Result(0, "修改后的密码不能和原来的一致", null);
        }
        return new Result(0, "修改失败", null);
    }


}
